﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Jocker;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using NPOI.OpenXmlFormats.Spreadsheet;

namespace ManagoMes.Read.API.Applications.WorkflowEngine
{
    public class WorkflowEngineHandler : IRequestHandler<WorkflowEngineCommond, ResultsDTO>
    {
        /// <summary>
        /// 业务流程审核表
        /// </summary>
        private IBateRepository<BusinessTable> _BusinessTable;
        /// <summary>
        /// 审批流程设置表
        /// </summary>
        private IBateRepository<WorkflowTable> _WorkflowTable;
        /// <summary>
        /// 审批流程表
        /// </summary>
        private IBateRepository<ApprovalProcess> _ApprovalProcess;

        public WorkflowEngineHandler(IBateRepository<BusinessTable> businessTable, IBateRepository<WorkflowTable> workflowTable, IBateRepository<ApprovalProcess> approvalProcess)
        {
            _BusinessTable = businessTable;
            _WorkflowTable = workflowTable;
            _ApprovalProcess = approvalProcess;
        }

        public async Task<ResultsDTO> Handle(WorkflowEngineCommond request, CancellationToken cancellationToken)
        {
            var list = new ResultsDTO() { Code=0};
            var Emety= new WorkflowTable();
            var str = "SELECT * from WorkflowTable";
            DapperHelper dapperHelper = new DapperHelper();
            var WorkflowTabledata = _WorkflowTable.GetAll(str,Emety, dapperHelper.MangoMesRead).FirstOrDefault(x=>x.workid==request.Workid);
           var newdate=  WorkflowTabledata.ExamineNameList.Split(',');
            var result = "";
            foreach (var item in newdate)
            {
                if(item==request.USerid.ToString() &&request.WorkType==1)
                {
                     result = "审核通过";
                    break;
                }
                else if(request.WorkType==0)
                {
                    result = "审核不通过";
                    list.Message = "审核不通过";
                    break;
                }
                if (request.USerid != 1)
                {
                    var Inquire = " SELECT * from ApprovalProcess WHERE Sequence='" + (request.USerid - 1) + "'and Typetable='"+request.Gnumid+"'";
                    var Empty = new ApprovalProcess();
                    var Firstordate = _ApprovalProcess.GetAll(Inquire, Empty, dapperHelper.MangoMesRead).FirstOrDefault();
                    if (item == request.USerid.ToString() && Firstordate.SeqeResult == "审核通过" && Firstordate!=null)
                    {
                        if (request.WorkType == 1)
                        {
                            result = "审核通过";
                            break;
                        }
                        else
                        {
                            result = "审核失败";
                            break;
                        }
                    }
                    else
                    {
                        list.Message = "不能跨级批准请联系上一级审批人";
                        break;
                    }
                }
            }         
            if(result!="")
            {
                var SeqeMEssage = "";
                if (result== "审核通过")
                {
                    SeqeMEssage = "同意";
                    list.Message = "审批通过";
                }
                else
                {
                    SeqeMEssage = "不同意";
                    list.Message = "审批失败";
                }

                var Add = new ApprovalProcess()
                {
                    Applyid = WorkflowTabledata.workid,
                    ApplyName = request.USerid,
                    Sequence = request.USerid,
                    SeqeTime = DateTime.Now,
                    SeqeResult = result,
                    SeqeMEssage = "",
                    Typetable=request.Gnumid,
                };
                var conten = " insert ApprovalProcess VALUES(@id,@Applyid,@ApplyName,@Sequence,@SeqeTime,@SeqeResult,@SeqeMEssage,@Typetable)";
                var Success = await _ApprovalProcess.Add(conten, Add, dapperHelper.MangoMesWrite);
                
            }
           
            return list;

        }
    }


}
